﻿using ZuesControls.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using ZuesControls.Helpers;
using ZuesControls.Interfaces;

namespace ZuesControls.Controls
{
    public class ZSMessage : ContentControl, IMessage
    {
        public Button PART_BtnYes { get; set; }
        public Button PART_BtnOK { get; set; }
        public Button PART_BtnNo { get; set; }
        public Button PART_BtnCancel { get; set; }

        public Window ParentWindow;
        public Border ParentBorder;

        public Border PART_Border { get; set; }

        public TaskCompletionSource<MessageBoxResult> MessageBoxResultTCS { get; set; }
        static ZSMessage()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ZSMessage), new FrameworkPropertyMetadata(typeof(ZSMessage)));
        }



        public static readonly DependencyProperty CaptionProperty =
            DependencyProperty.Register("Caption", typeof(string), typeof(ZSMessage), new PropertyMetadata("这是ZS的消息框"));

        public static readonly DependencyProperty MessageBoxTextProperty =
            DependencyProperty.Register("MessageBoxText", typeof(string), typeof(ZSMessage), new PropertyMetadata("空"));


        public static readonly DependencyProperty MessageBoxButtonProperty =
            DependencyProperty.Register("MessageBoxButton", typeof(MessageBoxButton), typeof(ZSMessage), new PropertyMetadata(default));

        public static readonly DependencyProperty MessageBoxImageProperty =
             DependencyProperty.Register("MessageBoxImage", typeof(MessageBoxImage), typeof(ZSMessage), new PropertyMetadata(default));


        public static readonly DependencyProperty DefaultResultProperty =
            DependencyProperty.Register("DefaultResult", typeof(MessageBoxResult), typeof(ZSMessage), new PropertyMetadata(default));


        public static readonly DependencyProperty OptionsProperty =
            DependencyProperty.Register("Options", typeof(MessageBoxOptions), typeof(ZSMessage), new PropertyMetadata(default));


        // Using a DependencyProperty as the backing store for IsShowMessage.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsShowMessageProperty =
            DependencyProperty.Register("IsShowMessage", typeof(bool), typeof(ZSMessage), new PropertyMetadata(false));

        [Description("消息框标题")]
        [Category("消息框样式设置")]
        [Browsable(true)]
        public string Caption
        {
            get { return (string)GetValue(CaptionProperty); }
            set { SetValue(CaptionProperty, value); }
        }


        [Description("动画")]
        [Category("设置弹出动画")]
        public bool IsShowMessage
        {
            get { return (bool)GetValue(IsShowMessageProperty); }
            set { SetValue(IsShowMessageProperty, value); }
        }


        [Description("消息框内容")]
        [Category("消息框样式设置")]
        [Browsable(true)]
        public string MessageBoxText
        {
            get { return (string)GetValue(MessageBoxTextProperty); }
            set { SetValue(MessageBoxTextProperty, value); }
        }

        [Description("消息框按钮")]
        [Category("消息框样式设置")]
        [Browsable(true)]
        public MessageBoxButton MessageBoxButton
        {
            get { return (MessageBoxButton)GetValue(MessageBoxButtonProperty); }
            set { SetValue(MessageBoxButtonProperty, value); }
        }

        [Description("消息提示图标")]
        [Category("消息框样式设置")]
        [Browsable(true)]
        public MessageBoxImage MessageBoxImage
        {
            get { return (MessageBoxImage)GetValue(MessageBoxImageProperty); }
            set { SetValue(MessageBoxImageProperty, value); }
        }


        [Description("消息框默认返回消息")]
        [Category("消息框样式设置")]
        [Browsable(true)]
        public MessageBoxResult DefaultResult
        {
            get { return (MessageBoxResult)GetValue(DefaultResultProperty); }
            set { SetValue(DefaultResultProperty, value); }
        }

        [Description("指定消息框的特殊显示选项")]
        [Category("消息框样式设置")]
        [Browsable(true)]
        public MessageBoxOptions Options
        {
            get { return (MessageBoxOptions)GetValue(OptionsProperty); }
            set { SetValue(OptionsProperty, value); }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.PART_BtnYes = this.GetTemplateChild(nameof(this.PART_BtnYes)) as Button;
            this.PART_BtnOK = this.GetTemplateChild(nameof(this.PART_BtnOK)) as Button;
            this.PART_BtnNo = this.GetTemplateChild(nameof(this.PART_BtnNo)) as Button;
            this.PART_BtnCancel = this.GetTemplateChild(nameof(this.PART_BtnCancel)) as Button;
            this.PART_Border = this.GetTemplateChild(nameof(this.PART_Border)) as Border;
            ((Interfaces.IMessage)this).HandleBtnClickEvent();
        }
        public void HandleBtnClickEvent()
        {
            if (this.PART_BtnYes != null)
            {
                this.PART_BtnYes.Click += (sender, e) =>
                {
                    this.SetMessageBoxResult(MessageBoxResult.Yes);
                };
            }

            if (this.PART_BtnOK != null)
            {
                this.PART_BtnOK.Click += (sender, e) =>
                {
                    this.SetMessageBoxResult(MessageBoxResult.OK);
                };
            }
            if (this.PART_BtnNo != null)
            {
                this.PART_BtnNo.Click += (sender, e) =>
                {
                    this.SetMessageBoxResult(MessageBoxResult.No);
                };
            }
            if (this.PART_BtnCancel != null)
            {
                this.PART_BtnCancel.Click += (sender, e) =>
                {
                    this.SetMessageBoxResult(MessageBoxResult.Cancel);
                };
            }
        }


        private void SetMessageBoxResult(MessageBoxResult messageBoxResult)
        {
            this.MessageBoxResultTCS?.SetResult(messageBoxResult);
            this.MessageBoxClose();
        }

        public void MessageBoxDisplay(Window window)
        {
            if ( PART_Border == null)
            {
                this.ApplyTemplate();
            }
            this.ParentBorder ??= VisualHelper.TryFindParent<Border>(this);
            this.PART_Border.Visibility = Visibility.Visible;
            this.ParentBorder.Visibility = Visibility.Visible;
            this.IsShowMessage = true;
            //var sb = new Storyboard();
            //var opacityAnim = new DoubleAnimation
            //{
            //    From = 0,
            //    To = 1,
            //    Duration = TimeSpan.FromMilliseconds(300)
            //};
            //Storyboard.SetTarget(opacityAnim, this.PART_Border);
            //Storyboard.SetTargetProperty(opacityAnim, new PropertyPath("Opacity"));
            //sb.Children.Add(opacityAnim);
            //var duration = TimeSpan.FromMilliseconds(300);
            //this.PART_Border.RenderTransformOrigin = new Point(0.5, 0.5);
            //if (this.PART_Border.RenderTransform is not ScaleTransform scale)
            //{
            //    scale = new ScaleTransform(0, 0); // 初始为 (0,0) 缩放
            //    this.PART_Border.RenderTransform = scale;
            //}
            //// 创建 Y 方向动画
            //var scaleYAnim = new DoubleAnimation
            //{
            //    From = 0,
            //    To = 1,
            //    Duration = duration,
            //    AccelerationRatio = 0.2,
            //    DecelerationRatio = 0.8
            //};
            //// 创建 X 方向动画
            //var scaleXAnim = new DoubleAnimation
            //{
            //    From = 0,
            //    To = 1,
            //    Duration = duration,
            //    AccelerationRatio = 0.2,
            //    DecelerationRatio = 0.8
            //};
            //Storyboard.SetTarget(scaleXAnim, this.PART_Border);
            //Storyboard.SetTarget(scaleYAnim, this.PART_Border);
            //Storyboard.SetTargetProperty(scaleXAnim, new PropertyPath("RenderTransform.ScaleX"));
            //Storyboard.SetTargetProperty(scaleYAnim, new PropertyPath("RenderTransform.ScaleY"));
            //sb.Children.Add(scaleXAnim);
            //sb.Children.Add(scaleYAnim);
            //// 启动动画
            //sb.Begin();
        }

        public void MessageBoxClose()
        {
            //var sb = new Storyboard();
            //var duration = TimeSpan.FromMilliseconds(300);
            //// 透明度动画：从 1 → 0
            //var opacityAnim = new DoubleAnimation
            //{
            //    From = 1,
            //    To = 0,
            //    Duration = duration
            //};
            //Storyboard.SetTarget(opacityAnim, this.PART_Border);
            //Storyboard.SetTargetProperty(opacityAnim, new PropertyPath("Opacity"));
            //sb.Children.Add(opacityAnim);
            //this.PART_Border.RenderTransformOrigin = new Point(0.5, 0.5);
            //if (this.PART_Border.RenderTransform is not ScaleTransform scale)
            //{
            //    scale = new ScaleTransform(1, 1);
            //    this.PART_Border.RenderTransform = scale;
            //}
            //// ScaleY 动画：从 1 → 0
            //var scaleYAnim = new DoubleAnimation
            //{
            //    From = 1,
            //    To = 0,
            //    Duration = duration,
            //    AccelerationRatio = 0.2,
            //    DecelerationRatio = 0.8
            //};

            //// ScaleX 动画：从 1 → 0
            //var scaleXAnim = new DoubleAnimation
            //{
            //    From = 1,
            //    To = 0,
            //    Duration = duration,
            //    AccelerationRatio = 0.2,
            //    DecelerationRatio = 0.8
            //};

            //Storyboard.SetTarget(scaleXAnim, this.PART_Border);
            //Storyboard.SetTarget(scaleYAnim, this.PART_Border);
            //Storyboard.SetTargetProperty(scaleXAnim, new PropertyPath("RenderTransform.ScaleX"));
            //Storyboard.SetTargetProperty(scaleYAnim, new PropertyPath("RenderTransform.ScaleY"));
            //sb.Children.Add(scaleXAnim);
            //sb.Children.Add(scaleYAnim);
            //// 动画完成后隐藏控件
            //sb.Completed += (s, e) =>
            //{
            //    this.PART_Border.Visibility = Visibility.Collapsed;
            //    if (this.ParentBorder != null)
            //        this.ParentBorder.Visibility = Visibility.Collapsed;
            //};

            //sb.Begin();
            this.IsShowMessage = false;
            this.PART_Border.Visibility = Visibility.Collapsed;
            if (this.ParentBorder != null)
                this.ParentBorder.Visibility = Visibility.Collapsed;
        }

        public async Task<MessageBoxResult> ShowMessageAsync(Window window, string messageBoxText = "default", string caption = null, MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxOptions options = MessageBoxOptions.None)
        {
            this.MessageBoxResultTCS = new TaskCompletionSource<MessageBoxResult>();
            if (string.IsNullOrWhiteSpace(caption))
            {
                caption = "消息提示";
            }
            Dispatcher.Invoke(() =>
            {
                this.MessageBoxText = messageBoxText;
                this.Caption = caption;
                this.MessageBoxButton = button;
                this.MessageBoxImage = icon;
                this.DefaultResult = defaultResult;
                this.Options = options;
                this.MessageBoxDisplay(window);
            });
            return await this.MessageBoxResultTCS.Task;
        }

        public async Task<MessageBoxResult> ShowMessageAsync(string messageBoxText)
        {
            return await ShowMessageAsync(null,
                            messageBoxText: messageBoxText,
                            caption: null,
                            button: MessageBoxButton.OK,
                            icon: MessageBoxImage.None,
                            defaultResult: MessageBoxResult.None,
                            options: MessageBoxOptions.None);
        }

        public Task<MessageBoxResult> ShowMessageAsync(string messageBoxText, string caption)
        {
            throw new NotImplementedException();
        }

        public Task<MessageBoxResult> ShowMessageAsync(string messageBoxText, string caption, MessageBoxButton button)
        {
            throw new NotImplementedException();
        }

        public Task<MessageBoxResult> ShowMessageAsync(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
        {
            throw new NotImplementedException();
        }

        public Task<MessageBoxResult> ShowMessageAsync(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
        {
            throw new NotImplementedException();
        }

        public Task<MessageBoxResult> ShowMessageAsync(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult, MessageBoxOptions options)
        {
            throw new NotImplementedException();
        }

        public async Task<MessageBoxResult> ShowMessageAsync(Window window, string messageBoxText)
        {
            return await ShowMessageAsync(window,
                           messageBoxText: messageBoxText,
                           caption: null,
                           button: MessageBoxButton.OK,
                           icon: MessageBoxImage.None,
                           defaultResult: MessageBoxResult.None,
                           options: MessageBoxOptions.None);
        }

        public Task<MessageBoxResult> ShowMessageAsync(Window window, string messageBoxText, string caption)
        {
            throw new NotImplementedException();
        }

        public async Task<MessageBoxResult> ShowMessageAsync(Window window, string messageBoxText, string caption, MessageBoxButton button)
        {
            return await ShowMessageAsync(window,
                            messageBoxText: messageBoxText,
                            caption: caption,
                            button: button,
                            icon: MessageBoxImage.None,
                            defaultResult: MessageBoxResult.None,
                            options: MessageBoxOptions.None);
        }

        public Task<MessageBoxResult> ShowMessageAsync(Window window, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
        {
            throw new NotImplementedException();
        }

        public Task<MessageBoxResult> ShowMessageAsync(Window window, string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon, MessageBoxResult defaultResult)
        {
            throw new NotImplementedException();
        }
    }
}
